﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utem.Command;

namespace Utem.Server
{
    public partial class FormProjector : Form
    {
        private const int WM_NCHITTEST = 0x84;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;

        private readonly Image[] ansImages;

        private Timer timer;
        private int counter = 0;

        // Handling the window messages
        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);

            if (message.Msg == WM_NCHITTEST && (int) message.Result == HTCLIENT)
            {
                message.Result = (IntPtr)HTCAPTION;
            }
        }

        public FormProjector()
        {
            InitializeComponent();

            // Change timer font.
            var fontsCollection = new PrivateFontCollection();
            fontsCollection.AddFontFile(Application.StartupPath + @"\Fonts\DS-DIGIB.TTF");
            var digiFont = new Font(fontsCollection.Families[0], 28, FontStyle.Bold);
            lbTimer.Font = digiFont;

            // Prepare images.
            ansImages = new Image[4];
            for (var i = 0; i < 4; i++)
            {
                ansImages[i] = Properties.Resources.normal;
            }

            // Initial timer.
            timer = new Timer {Interval = 1000};
            timer.Stop();
            timer.Tick += timer_Tick;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            counter--;
            lbTimer.Text = counter.ToString().PadLeft(2, '0');
            if (counter <= 0)
            {
                timer.Stop();
            }
        }

        public void StartTimer(int secs)
        {
            counter = secs;
            timer.Start();
        }

        public void SetQuestion(Question q, int num)
        {
            lbA.Enabled = true;
            lbB.Enabled = true;
            lbC.Enabled = true;
            lbD.Enabled = true;

            lbAnsA.Enabled = true;
            lbAnsB.Enabled = true;
            lbAnsC.Enabled = true;
            lbAnsD.Enabled = true;

            ansImages[0] = Properties.Resources.normal;
            ansImages[1] = Properties.Resources.normal;
            ansImages[2] = Properties.Resources.normal;
            ansImages[3] = Properties.Resources.normal;

            lbQNum.Text = num + "";
            lbQuestion.Text = q.Content;
            lbAnsA.Text = q.AnsA;
            lbAnsB.Text = q.AnsB;
            lbAnsC.Text = q.AnsC;
            lbAnsD.Text = q.AnsD;
        }

        public void SetCorrectAns(char ans)
        {
            switch (ans)
            {
                case 'A':
                    lbB.Enabled = false;
                    lbC.Enabled = false;
                    lbD.Enabled = false;
                    lbAnsB.Enabled = false;
                    lbAnsC.Enabled = false;
                    lbAnsD.Enabled = false;
                    ansImages[0] = Properties.Resources.correct;
                    ansImages[1] = Properties.Resources.wrong;
                    ansImages[2] = Properties.Resources.wrong;
                    ansImages[3] = Properties.Resources.wrong;
                    break;
                case 'B':
                    lbA.Enabled = false;
                    lbC.Enabled = false;
                    lbD.Enabled = false;
                    lbAnsA.Enabled = false;
                    lbAnsC.Enabled = false;
                    lbAnsD.Enabled = false;
                    ansImages[0] = Properties.Resources.wrong;
                    ansImages[1] = Properties.Resources.correct;
                    ansImages[2] = Properties.Resources.wrong;
                    ansImages[3] = Properties.Resources.wrong;
                    break;
                case 'C':
                    lbA.Enabled = false;
                    lbB.Enabled = false;
                    lbD.Enabled = false;
                    lbAnsA.Enabled = false;
                    lbAnsB.Enabled = false;
                    lbAnsD.Enabled = false;
                    ansImages[0] = Properties.Resources.wrong;
                    ansImages[1] = Properties.Resources.wrong;
                    ansImages[2] = Properties.Resources.correct;
                    ansImages[3] = Properties.Resources.wrong;
                    break;
                case 'D':
                    lbA.Enabled = false;
                    lbB.Enabled = false;
                    lbC.Enabled = false;
                    lbAnsA.Enabled = false;
                    lbAnsB.Enabled = false;
                    lbAnsC.Enabled = false;
                    ansImages[0] = Properties.Resources.wrong;
                    ansImages[1] = Properties.Resources.wrong;
                    ansImages[2] = Properties.Resources.wrong;
                    ansImages[3] = Properties.Resources.correct;
                    break;
            }
            Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawImage(Properties.Resources.qa, 32, 205, 736, 283);

            e.Graphics.DrawImage(ansImages[0], 44, 300, 49, 36);
            e.Graphics.DrawImage(ansImages[1], 44, 348, 49, 36);
            e.Graphics.DrawImage(ansImages[2], 44, 396, 49, 36);
            e.Graphics.DrawImage(ansImages[3], 44, 444, 49, 36);
        }
    }
}
